package com.sc.nft.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.sc.nft.dao.*;
import com.sc.nft.dto.UserAuctionBiddingDTO;
import com.sc.nft.dto.UserAuctionBiddingDetailDTO;
import com.sc.nft.entity.*;
import com.sc.nft.entity.dto.AuctionConsumeAdminPageDTO;
import com.sc.nft.entity.dto.AuctionRecordAdminDTO;
import com.sc.nft.entity.dto.AuctionUserBiddingAdminPageDTO;
import com.sc.nft.entity.vo.*;
import com.sc.nft.enums.ActivityStatusEnum;
import com.sc.nft.enums.AcutionParticipationTypeEnum;
import com.sc.nft.enums.AcutionProgressEnum;
import com.sc.nft.enums.AuctionAdminStatusEnum;
import com.sc.nft.enums.UserCollectionFreezingStatusEnum;
import com.sc.nft.enums.UserEquityPropsUseTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.DigitalCollectionProductInfoAuctionService;
import com.sc.nft.service.EquityPropsService;
import com.sc.nft.service.UserCollectionService;
import com.sc.nft.service.UserEquityPropsService;
import com.sc.nft.sup.ErrorCode;
import com.tencentcloudapi.dlc.v20210125.models.Asset;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import javax.validation.Valid;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 拍卖配置表 服务实现类
 * </p>
 *
 * @author liGen
 * @since 2024-09-05
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class DigitalCollectionProductInfoAuctionServiceImpl implements DigitalCollectionProductInfoAuctionService {

    private final DigitalCollectionProductInfoAuctionDao digitalCollectionProductInfoAuctionDao;
    private final DigitalCollectionProductInfoAuctionUserBiddingDao digitalCollectionProductInfoAuctionUserBiddingDao;
    private final DigitalCollectionProductInfoAuctionUserBiddingResultsDao digitalCollectionProductInfoAuctionUserBiddingResultsDao;
    private final UserEquityPropsService userEquityPropsService;
    private final UserCollectionService userCollectionService;
    private final DigitalCollectionProductInfoDao digitalCollectionProductInfoDao;
    private final DigitalCollectionProductInfoAuctionConditionDao digitalCollectionProductInfoAuctionConditionDao;
    private final DigitalCollectionProductInfoAuctionConsumptionConditionDao digitalCollectionProductInfoAuctionConsumptionConditionDao;
    private final UserCollectionDao userCollectionDao;
    private final UserEquityPropsDao userEquityPropsDao;
    private final DigitalCollectionProductInfoAuctionUserBiddingDetailsDao digitalCollectionProductInfoAuctionUserBiddingDetailsDao;
    private final StaticDataDao staticDataDao;
    private final DigitalCollectionProductInfoAuctionUserAgreementDao digitalCollectionProductInfoAuctionUserAgreementDao;

    /**
     * 用户拍卖记录
     *
     * @param userId
     * @param mp
     * @return
     */
    @Override
    public IPage<UserAuctionRecordPageVO> auctionRecords(Long userId, Page mp) {
        return digitalCollectionProductInfoAuctionUserBiddingDao.auctionRecords(userId, mp);
    }

    /**
     * 单个拍卖详情
     *
     * @param userId
     * @param productId
     * @return
     */
    @Override
    public UserAuctionRecordDetailsVO auctionDetails(Long userId, Long productId) {
        UserAuctionRecordDetailsVO userAuctionRecordDetailsVO = new UserAuctionRecordDetailsVO();
        List<DigitalCollectionProductInfoAuctionUserBidding> biddingList = digitalCollectionProductInfoAuctionUserBiddingDao.listByProductIdAndUserId(userId, productId);
        if (CollectionUtils.isEmpty(biddingList)) {
            return userAuctionRecordDetailsVO;
        }
        userAuctionRecordDetailsVO.setBiddingTotal(biddingList.stream().mapToInt(DigitalCollectionProductInfoAuctionUserBidding::getBiddingNum).sum());
        //是否开奖了
        DigitalCollectionProductInfoAuctionUserBiddingResults results = digitalCollectionProductInfoAuctionUserBiddingResultsDao.getByProductIdAndUserId(userId, productId);
        if (Objects.isNull(results)) {
            userAuctionRecordDetailsVO.setProgress(AcutionProgressEnum.ONGOING);
        } else {
            userAuctionRecordDetailsVO.setProgress(results.getProgress());

            if (null == results.getBiddingTotal() || results.getBiddingTotal() == 0) {
                userAuctionRecordDetailsVO.setProgress(null);
            }
        }

        List<UserAuctionRecordDetailsChildVO> biddingInfo = Lists.newArrayList();
        for (DigitalCollectionProductInfoAuctionUserBidding digitalCollectionProductInfoAuctionUserBidding : biddingList) {
            UserAuctionRecordDetailsChildVO userAuctionRecordDetailsChildVO = new UserAuctionRecordDetailsChildVO();
            userAuctionRecordDetailsChildVO.setBiddingNum(digitalCollectionProductInfoAuctionUserBidding.getBiddingNum());
            userAuctionRecordDetailsChildVO.setTitle(digitalCollectionProductInfoAuctionUserBidding.getFirstTitle());
            userAuctionRecordDetailsChildVO.setCreateTime(digitalCollectionProductInfoAuctionUserBidding.getCreateTime());
            biddingInfo.add(userAuctionRecordDetailsChildVO);
        }

        userAuctionRecordDetailsVO.setBiddingInfo(biddingInfo);
        return userAuctionRecordDetailsVO;
    }

    /**
     * 我的质押物
     *
     * @param userId
     * @param productId
     * @return
     */
    @Override
    public UserAuctionPledgeVO auctionPledgeItems(Long userId, Long productId) {
        UserAuctionPledgeVO userAuctionPledgeVO = new UserAuctionPledgeVO();

        List<DigitalCollectionProductInfoAuctionUserBidding> biddingList = digitalCollectionProductInfoAuctionUserBiddingDao.listByProductIdAndUserId(userId, productId);
        if (CollectionUtils.isEmpty(biddingList)) {
            return userAuctionPledgeVO;
        }
        userAuctionPledgeVO.setPledgeTotal(biddingList.stream().mapToInt(DigitalCollectionProductInfoAuctionUserBidding::getBiddingNum).sum());
        List<UserAuctionPledgeChildVO> pledgeItems = Lists.newArrayList();
        for (DigitalCollectionProductInfoAuctionUserBidding digitalCollectionProductInfoAuctionUserBidding : biddingList) {
            UserAuctionPledgeChildVO userAuctionPledgeChildVO = new UserAuctionPledgeChildVO();
            userAuctionPledgeChildVO.setBiddingNum(digitalCollectionProductInfoAuctionUserBidding.getBiddingNum());
            userAuctionPledgeChildVO.setTitle(digitalCollectionProductInfoAuctionUserBidding.getFirstTitle());
            userAuctionPledgeChildVO.setPledgeImage(digitalCollectionProductInfoAuctionUserBidding.getPledgeImage());
            pledgeItems.add(userAuctionPledgeChildVO);
        }
        userAuctionPledgeVO.setPledgeItems(pledgeItems);
        return userAuctionPledgeVO;
    }

    /**
     * 中标结果公示
     *
     * @param productId
     * @param mp
     * @return
     */
    @Override
    public IPage<UserAuctionWinVO> auctionWinList(Long productId, Page mp) {
        return digitalCollectionProductInfoAuctionUserBiddingResultsDao.auctionWinList(productId, mp);
    }


    /**
     * 去参加拍卖活动
     *
     * @param productId
     * @param userId
     * @return
     */
    @Override
    public UserAuctionParticipationVO auctionParticipation(Long productId, Long userId) {
        DigitalCollectionProductInfo productInfo = digitalCollectionProductInfoDao.getById(productId);
        if (Objects.isNull(productInfo)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_NOT_FOUND);
        }
        DigitalCollectionProductInfoAuction collectionProductInfoAuction = digitalCollectionProductInfoAuctionDao.getByProductId(productId);
        if (Objects.isNull(collectionProductInfoAuction)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_NOT_FOUND);
        }
        if (collectionProductInfoAuction.getAuctionStartTime().after(DateTime.now())) {
            throw new GlobalRunTimeException(ErrorCode.NOT_BEGIN);
        }
        if (collectionProductInfoAuction.getAuctionEndTime().before(DateTime.now())) {
            throw new GlobalRunTimeException(ErrorCode.ALREADY_END);
        }
        //参与条件
        List<DigitalCollectionProductInfoAuctionCondition> conditionList = digitalCollectionProductInfoAuctionConditionDao.listByProductId(productId);
        if (CollectionUtils.isEmpty(conditionList)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_NOT_FOUND);
        }
        //质押物品
        List<DigitalCollectionProductInfoAuctionConsumptionCondition> consumptionConditionList = digitalCollectionProductInfoAuctionConsumptionConditionDao.listByProductId(productId);
        if (CollectionUtils.isEmpty(consumptionConditionList)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_NOT_FOUND);
        }
        //是否可以参与
        Boolean canParticipate = Boolean.FALSE;
        //确认还是追加
        Boolean auctionFirstParticipation = Boolean.FALSE;
        //用户投入数量
        Map<Long, Integer> userBiddingMap = new HashMap<>();
        List<DigitalCollectionProductInfoAuctionUserBidding> userBiddingList = digitalCollectionProductInfoAuctionUserBiddingDao.listByProductIdAndUserId(userId, productId);
        if (!CollectionUtils.isEmpty(userBiddingList)) {
            //判断是否是第一次参与,存在投入记录不是第一次参加
            auctionFirstParticipation = Boolean.TRUE;
            userBiddingMap = userBiddingList.stream().collect(Collectors.groupingBy(DigitalCollectionProductInfoAuctionUserBidding::getAuctionConsumptionConditionId, Collectors.summingInt(DigitalCollectionProductInfoAuctionUserBidding::getBiddingNum)));
        }

        List<UserAuctionParticipationConditionVO> neddConditionList = Lists.newArrayList();

        //判断是否满足参与条件
        HashSet<Boolean> booleans = new HashSet<>();
        for (DigitalCollectionProductInfoAuctionCondition condition : conditionList) {
            UserAuctionParticipationConditionVO userAuctionParticipationConditionVO = new UserAuctionParticipationConditionVO();
            userAuctionParticipationConditionVO.setNum(condition.getNum());
            userAuctionParticipationConditionVO.setFirstTitle(condition.getFirstTitle());
            if (collectionProductInfoAuction.getParticipationType() == AcutionParticipationTypeEnum.CONSUMPTION_PROPS) {
                Integer havingCount = userEquityPropsService.getHavingCountByUserId(condition.getRelationId(), userId);
                //如果是第一次需要判断消费的门槛
                if (!auctionFirstParticipation) {
                    if (havingCount != null && havingCount >= condition.getNum()) {
                        booleans.add(Boolean.TRUE);
                    } else {
                        booleans.add(Boolean.FALSE);
                    }
                }
                userAuctionParticipationConditionVO.setHoldNum(havingCount == null ? 0 : havingCount);
            } else if (collectionProductInfoAuction.getParticipationType() == AcutionParticipationTypeEnum.HOLD_COLLECTION) {
                Integer userCount = userCollectionDao.getCountByCollectionId(condition.getRelationId(), userId);
                if (userCount != null && userCount >= condition.getNum()) {
                    booleans.add(Boolean.TRUE);
                } else {
                    booleans.add(Boolean.FALSE);
                }
                userAuctionParticipationConditionVO.setHoldNum(userCount == null ? 0 : userCount);
            }
            neddConditionList.add(userAuctionParticipationConditionVO);
        }
        //消耗的数量
        List<UserAuctionParticipationConsumptionVO> pledgeItems = Lists.newArrayList();
        //需要消耗的条件
        //如过是第一次需要判断持有的总数量
        //用户所有可以质押总数
        Integer userCanPledgeNum = 0;
        for (DigitalCollectionProductInfoAuctionConsumptionCondition consumptionCondition : consumptionConditionList) {
            UserAuctionParticipationConsumptionVO consumptionVO = new UserAuctionParticipationConsumptionVO();
            consumptionVO.setAlreadyPledgeNum(userBiddingMap.get(consumptionCondition.getId()) == null ? 0 : userBiddingMap.get(consumptionCondition.getId()));
            consumptionVO.setFirstTitle(consumptionCondition.getFirstTitle());
            consumptionVO.setPledgeImage(consumptionCondition.getPledgeImage());
            consumptionVO.setConsumptionConditionId(consumptionCondition.getId());
            if (consumptionCondition.getParticipationType() == AcutionParticipationTypeEnum.CONSUMPTION_PROPS) {
                Integer userEquityPropsCount = userEquityPropsService.getHavingCountByUserId(consumptionCondition.getRelationId(), userId);
                consumptionVO.setHoldNum(userEquityPropsCount);
                userCanPledgeNum += userEquityPropsCount == null ? 0 : userEquityPropsCount;
            } else if (consumptionCondition.getParticipationType() == AcutionParticipationTypeEnum.HOLD_COLLECTION) {
                Integer userCount = userCollectionDao.getCountByCollectionId(consumptionCondition.getRelationId(), userId);
                consumptionVO.setHoldNum(userCount);
                userCanPledgeNum += userCount == null ? 0 : userCount;
            }
            pledgeItems.add(consumptionVO);
        }
        //如果是第一次需要判断总数量
        if (!auctionFirstParticipation) {
            if (userCanPledgeNum >= collectionProductInfoAuction.getConsumptionNum()) {
                booleans.add(Boolean.TRUE);
            } else {
                booleans.add(Boolean.FALSE);
            }
        } else {
            if (userCanPledgeNum > 0) {
                booleans.add(Boolean.TRUE);
            } else {
                booleans.add(Boolean.FALSE);
            }
        }
        if (booleans.contains(Boolean.FALSE)) {
            canParticipate = Boolean.FALSE;
        } else {
            canParticipate = Boolean.TRUE;
        }
        UserAuctionParticipationVO userAuctionParticipationVO = new UserAuctionParticipationVO();
        userAuctionParticipationVO.setProductId(productId);
        userAuctionParticipationVO.setMinPledgeNum(collectionProductInfoAuction.getConsumptionNum());
        userAuctionParticipationVO.setParticipationType(collectionProductInfoAuction.getParticipationType());
        userAuctionParticipationVO.setCanParticipate(canParticipate);
        userAuctionParticipationVO.setAuctionFirstParticipation(auctionFirstParticipation);
        userAuctionParticipationVO.setConditionList(neddConditionList);
        userAuctionParticipationVO.setPledgeItems(pledgeItems);
        return userAuctionParticipationVO;
    }


    /**
     * 用户投标
     *
     * @param userAuctionBiddingDTO
     * @param user
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void auctionBidding(UserAuctionBiddingDTO userAuctionBiddingDTO, UserInfo user) {
        DigitalCollectionProductInfo productInfo = digitalCollectionProductInfoDao.getById(userAuctionBiddingDTO.getProductId());
        if (Objects.isNull(productInfo)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_NOT_FOUND);
        }
        DigitalCollectionProductInfoAuction collectionProductInfoAuction = digitalCollectionProductInfoAuctionDao.getByProductId(userAuctionBiddingDTO.getProductId());
        if (Objects.isNull(collectionProductInfoAuction)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_NOT_FOUND);
        }
        if (collectionProductInfoAuction.getAuctionStartTime().after(DateTime.now())) {
            throw new GlobalRunTimeException(ErrorCode.NOT_BEGIN);
        }
        if (collectionProductInfoAuction.getAuctionEndTime().before(DateTime.now())) {
            throw new GlobalRunTimeException(ErrorCode.ALREADY_END);
        }
        //参与条件
        List<DigitalCollectionProductInfoAuctionCondition> conditionList = digitalCollectionProductInfoAuctionConditionDao.listByProductId(userAuctionBiddingDTO.getProductId());
        if (CollectionUtils.isEmpty(conditionList)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_NOT_FOUND);
        }
        //质押物品
        List<DigitalCollectionProductInfoAuctionConsumptionCondition> consumptionConditionList = digitalCollectionProductInfoAuctionConsumptionConditionDao.listByProductId(userAuctionBiddingDTO.getProductId());
        if (CollectionUtils.isEmpty(consumptionConditionList)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_NOT_FOUND);
        }
        //用户本次质押
        List<UserAuctionBiddingDetailDTO> pledgeItems = userAuctionBiddingDTO.getPledgeItems();
        int pledgeTotal = pledgeItems.stream().mapToInt(UserAuctionBiddingDetailDTO::getPledgeNum).sum();

        //确认还是追加
        Boolean auctionFirstParticipation = Boolean.FALSE;
        //用户投入数量
        Integer pledgeCount = digitalCollectionProductInfoAuctionUserBiddingDao.countByProductIdAndUserId(user.getId(), userAuctionBiddingDTO.getProductId());
        if (pledgeCount != null && pledgeCount > 0) {
            //判断是否是第一次参与,存在投入记录不是第一次参加
            auctionFirstParticipation = Boolean.TRUE;
        } else {
            //第一次参加判断本次质押数量是否大于等于配置的最低数量
            if (pledgeTotal < collectionProductInfoAuction.getConsumptionNum()) {
                throw new GlobalRunTimeException("未满足最低出价数量");
            }
        }

        //判断是否满足参与条件
        for (DigitalCollectionProductInfoAuctionCondition condition : conditionList) {
            if (collectionProductInfoAuction.getParticipationType() == AcutionParticipationTypeEnum.CONSUMPTION_PROPS) {
                //如果是第一次需要判断消费的门槛
                if (!auctionFirstParticipation) {
                    //直接消耗道具
                    Integer havingCount = userEquityPropsService.getHavingCountByUserId(condition.getRelationId(), user.getId());
                    Assert.isTrue(havingCount >= condition.getNum(), () -> new GlobalRunTimeException("道具持有量不足"));
                    List<UserEquityProps> userEquityPropsList = userEquityPropsService.getHavingListByUserIdAndEquityPropsIdAndLimit(user.getId(), condition.getRelationId(), condition.getNum());
                    Assert.isTrue(userEquityPropsList.size() == condition.getNum(), () -> new GlobalRunTimeException("质押失败,请稍后重试!"));

                    Assert.isTrue(
                            userEquityPropsService.normalToconsumeByIds(userEquityPropsList, user.getId(), productInfo.getFirstTitle(), productInfo.getId(), UserEquityPropsUseTypeEnum.AUCTION_CONSUMPTION)
                            , () -> new GlobalRunTimeException("质押失败,请稍后重试!"));
                    //生成扣减记录
                    for (UserEquityProps userEquityProps : userEquityPropsList) {
                        DigitalCollectionProductInfoAuctionUserCondition userCondition = new DigitalCollectionProductInfoAuctionUserCondition();
                        userCondition.setProductId(productInfo.getId());
                        userCondition.setUserId(user.getId());
                        userCondition.setNum(1);
                        userCondition.setFirstTitle(userEquityProps.getFirstTitle());
                        userCondition.setParticipationType(AcutionParticipationTypeEnum.CONSUMPTION_PROPS);
                        userCondition.setRelationId(userEquityProps.getId());
                        userCondition.insert();
                    }


                }
            } else if (collectionProductInfoAuction.getParticipationType() == AcutionParticipationTypeEnum.HOLD_COLLECTION) {
                Integer userCount = userCollectionDao.getCountByCollectionId(condition.getRelationId(), user.getId());
                if (userCount == null || userCount < condition.getNum()) {
                    throw new GlobalRunTimeException(StrUtil.format("{}持有数量不满足条件", condition.getFirstTitle()));

                }
            }
        }
        //质押商品是否充足
        Map<Long, DigitalCollectionProductInfoAuctionConsumptionCondition> consumptionConditionMap = consumptionConditionList.stream().collect(Collectors.toMap(DigitalCollectionProductInfoAuctionConsumptionCondition::getId, e -> e));

        List<UserCollection> allUserCollectionList = Lists.newArrayList();
        //判断是否满足质押数据条件

        List<DigitalCollectionProductInfoAuctionUserBiddingDetails> detailsList = Lists.newArrayList();
        for (UserAuctionBiddingDetailDTO pledgeItem : pledgeItems) {
            if (null == pledgeItem.getPledgeNum() || pledgeItem.getPledgeNum() <= 0) {
                continue;
            }
            if (!consumptionConditionMap.containsKey(pledgeItem.getConsumptionConditionId())) {
                throw new GlobalRunTimeException("质押失败,请稍后重试!");
            }
            DigitalCollectionProductInfoAuctionConsumptionCondition consumptionCondition = consumptionConditionMap.get(pledgeItem.getConsumptionConditionId());

            //用户投标记录表
            DigitalCollectionProductInfoAuctionUserBidding digitalCollectionProductInfoAuctionUserBidding = new DigitalCollectionProductInfoAuctionUserBidding();
            digitalCollectionProductInfoAuctionUserBidding.setProductId(productInfo.getId());
            digitalCollectionProductInfoAuctionUserBidding.setBiddingNum(pledgeItem.getPledgeNum());
            digitalCollectionProductInfoAuctionUserBidding.setUserId(user.getId());
            digitalCollectionProductInfoAuctionUserBidding.setRelationId(consumptionCondition.getRelationId());
            digitalCollectionProductInfoAuctionUserBidding.setFirstTitle(consumptionCondition.getFirstTitle());
            digitalCollectionProductInfoAuctionUserBidding.setParticipationType(consumptionCondition.getParticipationType());
            digitalCollectionProductInfoAuctionUserBidding.setAuctionConsumptionConditionId(consumptionCondition.getId());
            digitalCollectionProductInfoAuctionUserBidding.setPledgeImage(consumptionCondition.getPledgeImage());
            digitalCollectionProductInfoAuctionUserBidding.insert();

            if (consumptionCondition.getParticipationType() == AcutionParticipationTypeEnum.CONSUMPTION_PROPS) {
                //fixme 需要加权益道具质押状态
                Integer userEquityPropsCount = userEquityPropsService.getHavingCountByUserId(consumptionCondition.getRelationId(), user.getId());

            } else if (consumptionCondition.getParticipationType() == AcutionParticipationTypeEnum.HOLD_COLLECTION) {
                Integer userCount = userCollectionDao.getCountByCollectionId(consumptionCondition.getRelationId(), user.getId());
                List<UserCollection> userCollectionList = userCollectionDao.getUserCollectionListByCollectionIdAndUserIdAndSize(consumptionCondition.getRelationId(), user.getId(), pledgeItem.getPledgeNum());
                Assert.isTrue(userCollectionList.size() == pledgeItem.getPledgeNum(), () -> new GlobalRunTimeException(StrUtil.format("{}持有数量不满足条件", consumptionCondition.getFirstTitle())));
                allUserCollectionList.addAll(userCollectionList);
                for (UserCollection userCollection : userCollectionList) {
                    DigitalCollectionProductInfoAuctionUserBiddingDetails userBiddingDetails = new DigitalCollectionProductInfoAuctionUserBiddingDetails();
                    userBiddingDetails.setAuctionUserBiddingId(digitalCollectionProductInfoAuctionUserBidding.getId());
                    userBiddingDetails.setRelationId(userCollection.getId());
                    userBiddingDetails.setUserId(user.getId());
                    userBiddingDetails.setProductId(productInfo.getId());
                    userBiddingDetails.setParticipationType(consumptionCondition.getParticipationType());
                    detailsList.add(userBiddingDetails);
                }
            }

        }
        if(!auctionFirstParticipation && allUserCollectionList.size() < collectionProductInfoAuction.getConsumptionNum()){
            throw new GlobalRunTimeException("质押失败,持有未满足最低出价数量!");
        }
        if (CollectionUtils.isEmpty(allUserCollectionList) || allUserCollectionList.size() != pledgeTotal) {
            throw new GlobalRunTimeException("质押失败,请稍后重试!");
        }
        List<Long> userCollectionIds = allUserCollectionList.stream().map(UserCollection::getId).collect(Collectors.toList());
        //把藏品修改为质押状态
        Assert.isTrue(userCollectionDao.updateFreezingStatusByIds(userCollectionIds, UserCollectionFreezingStatusEnum.LOCK),()->new GlobalRunTimeException("质押失败"));
        Assert.isTrue(digitalCollectionProductInfoAuctionUserBiddingDetailsDao.batchSave(detailsList),()->new GlobalRunTimeException("质押失败"));

    }


    /**
     * 后台竞拍记录列表
     */

    @Override
    public Page<AuctionRecordAdminPageVO> getAuctionRecordAdminPage(AuctionRecordAdminDTO auctionRecordAdminDTO){
        Page<AuctionRecordAdminPageVO> auctionRecordAdminPageVOPage = digitalCollectionProductInfoAuctionDao.auctionRecordAdminPage(auctionRecordAdminDTO);
        List<AuctionRecordAdminPageVO> records = auctionRecordAdminPageVOPage.getRecords();
        if (ObjectUtil.isNotNull(records)) {
            records.forEach(record -> {
                if (record.getParticipationType().equals(AcutionParticipationTypeEnum.CONSUMPTION_PROPS)){
                    record.setParticipationTypeName(AcutionParticipationTypeEnum.CONSUMPTION_PROPS.getDescription());
                }else {
                    record.setParticipationTypeName(AcutionParticipationTypeEnum.HOLD_COLLECTION.getDescription());
                }
                if (new Date().before(record.getAuctionStartTime())) {
                    record.setAuctionStatus(AuctionAdminStatusEnum.NOT_START);
                    record.setAuctionStatusName(AuctionAdminStatusEnum.NOT_START.getDescription());
                } else if (new Date().after(record.getAuctionEndTime())) {
                    record.setAuctionStatus(AuctionAdminStatusEnum.END_AUCTION);
                    record.setAuctionStatusName(AuctionAdminStatusEnum.END_AUCTION.getDescription());
                } else {
                    record.setAuctionStatus(AuctionAdminStatusEnum.START_AUCTION);
                    record.setAuctionStatusName(AuctionAdminStatusEnum.START_AUCTION.getDescription());
                }
            });
        }
        return auctionRecordAdminPageVOPage;
    }


    /**
     * 拍卖数据汇总
     */
    @Override
    public AuctionDataDetailsVO auctionDataDetails(Long productId) {
        return digitalCollectionProductInfoAuctionUserBiddingResultsDao.auctionDataDetails(productId);
    }


    /**
     * 投标用户列表
     * @param auctionUserBiddingAdminPageDTO
     * @return
     */
    @Override
    public Page<AuctionUserBiddingAdminPageVO> getAuctionUserBiddingAdminPage(AuctionUserBiddingAdminPageDTO auctionUserBiddingAdminPageDTO) {
        Page<AuctionUserBiddingAdminPageVO> auctionUserBiddingAdminPage = digitalCollectionProductInfoAuctionUserBiddingResultsDao.getAuctionUserBiddingAdminPage(auctionUserBiddingAdminPageDTO.getProductId()
                , auctionUserBiddingAdminPageDTO.getUserTel(), auctionUserBiddingAdminPageDTO.getProgress(), auctionUserBiddingAdminPageDTO.toMp());
        List<AuctionUserBiddingAdminPageVO> records = auctionUserBiddingAdminPage.getRecords();
        for (AuctionUserBiddingAdminPageVO record : records) {
            if (record.getProgress().equals(AcutionProgressEnum.ONGOING)){
                record.setProgressName(AcutionProgressEnum.ONGOING.getDescription());
            }else if (record.getProgress().equals(AcutionProgressEnum.WINNING_BID)){
                record.setProgressName(AcutionProgressEnum.WINNING_BID.getDescription());
            }else {
                record.setProgressName(AcutionProgressEnum.NO_BID.getDescription());
            }
        }
        return auctionUserBiddingAdminPage;
    }

    /**
     * 获取用户协议
     *
     * @return
     */
    @Override
    public String auctionGetRule() {
        return staticDataDao.getByType("auction_rule").getValue();
    }

    /**
     * 用户同意协议
     *
     * @param productId
     * @param userId
     * @return
     */
    @Override
    public void auctionUserAgreement(Long productId, Long userId) {
        DigitalCollectionProductInfoAuctionUserAgreement digitalCollectionProductInfoAuctionUserAgreement = new DigitalCollectionProductInfoAuctionUserAgreement();
        digitalCollectionProductInfoAuctionUserAgreement.setProductId(productId);
        digitalCollectionProductInfoAuctionUserAgreement.setUserId(userId);
        digitalCollectionProductInfoAuctionUserAgreementDao.save(digitalCollectionProductInfoAuctionUserAgreement);
    }
}
